package com.example.child_protection.service.impl;

import com.example.child_protection.dao.CardDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.CardDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.CardInsertRequestDTO;
import com.example.child_protection.dto.response.CardListResponseDTO;
import com.example.child_protection.enums.CardStatesEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.CardModel;
import com.example.child_protection.service.CardManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 儿保卡号管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class CardManageServiceImpl extends MyBaseService implements CardManageService {

    @Autowired
    public CardDao cardDao;

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service (ServiceActionEnums.CARD_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                CardModel model = new CardModel();
                BeanUtils.copyProperties(requestDTO,model);
                cardDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 批量生成儿保卡号
     * @param requestDTO
     * @return
     */
    public CardListResponseDTO createCardBatch(CardInsertRequestDTO requestDTO){
        return this.service (ServiceActionEnums.CARD_CREATE_BATCH, requestDTO, CardListResponseDTO.class, new MyServiceExecutor<CardInsertRequestDTO, CardListResponseDTO>() {
            @Override
            protected CardListResponseDTO myprocess(CardInsertRequestDTO requestDTO) throws Exception {
                CardListResponseDTO res = new CardListResponseDTO();
                CardModel model = cardDao.getLastCardNo();
                Integer nums = requestDTO.getNums();
                CardModel newModel = new CardModel();
                String batchNo = new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date());
                List<CardDTO> infos = new ArrayList<>();
                CardDTO c = null;
                for(int i=1;i<=nums;i++){
                    newModel = new CardModel();
                    c = new CardDTO();
                    newModel.setCardNo( Integer.parseInt(model.getCardNo())+i+"");
                    newModel.setBatchNo(batchNo);
                    newModel.setCreatePersonId(requestDTO.getCreatePersonId());
                    newModel.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    cardDao.insert(newModel);
                    BeanUtils.copyProperties(newModel,c);
                    infos.add(c);
                }
                res.setInfos(infos);
                res.successfully();
                return res;
            }
        });
    }

}
